﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Conventions;

namespace Landscape.Business.FNH
{
    public class PropertyConvention : IPropertyConvention, IIdConvention
    {
        public void Apply(FluentNHibernate.Conventions.Instances.IPropertyInstance instance)
        {
            string tname = string.Empty;
            int idx = 0;
            foreach (char ltr in instance.Name)
            {
                if (ltr.Equals(ltr.ToString().ToUpper().ToCharArray()[0]))
                {
                    if (idx != 0)
                    {
                        tname += "_";
                    }
                    tname += ltr.ToString();
                }
                else
                {
                    tname += ltr.ToString().ToUpper();
                }
                idx++;
            }
            if (tname == string.Empty)
            {
                tname = instance.Name;
            }
            instance.Column(tname.ToUpper());

            switch (instance.EntityType.Name.ToLower())
            {
                case "role":
                    {
                        if (instance.Name == "Name")
                            instance.Unique();

                        break;
                    }

                case "user_master":
                    {
                        if (instance.Name == "StaffCode")
                            instance.Unique();

                        break;
                    }

                case "user_identity":
                    {
                        if (instance.Name == "LoginName")
                            instance.Unique();

                        break;
                    }
            }
        }

        public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
        {
            instance.UnsavedValue(Guid.Empty.ToString());
        }
    }
}
